import { defineStore } from "pinia";
import { logout as userLogout } from "@/api/user/index";
import { UserState } from "./types";

export const useUserStore = defineStore("user", {
	state: (): UserState => ({
		userName: undefined,
		nickName: undefined,
		userCode: undefined,
		userRole: undefined,
		userId: undefined,
		ftpInfo: undefined,
		ftpPwd: undefined,
	}),
	getters: {
		userProfile(state: UserState): UserState {
			return { ...state };
		},
	},
	actions: {
		// 设置用户的信息
		setInfo(partial: Partial<UserState>) {
			this.$patch(partial);
		},
		// 重置用户信息
		resetInfo() {
			this.$reset();
		},
		// Logout
		async logout() {
			await userLogout();
			this.resetInfo();
		},
	},
	persist: true,
});
